﻿using MyRoguelike.Tools;
using Microsoft.Xna.Framework;
using System;
using MyRoguelike.Game.Items;

namespace MyRoguelike.Game.Objects
{
    class GOSkeleton : GOCreature
    {
        public GOSkeleton(Level level) 
            : base(level)
        {
            Texture = ResourceManager.Textures["Skeleton"];

            IsEnemy = true;
            AggrRange = 50f;

            // Add weapon
            GameItem item = this.Inventory.AddItem(new GIRustySword());
            item.On();
        }

        public override void Step()
        {
            if (WalkPath.Count == 0)
            {
                // Go to random coords
                Random rnd = new Random();

                Point p = new Point(rnd.Next(_level.MapSize.X),
                    rnd.Next(_level.MapSize.Y));
                while (_level.Map[p.X, p.Y].Type != Level.EMapBlockType.Floor)
                    p = new Point(rnd.Next(_level.MapSize.X),
                        rnd.Next(_level.MapSize.Y));

                Go(p);
            }
            else
            {
                // If skeleton saw player or ally creatures that go to they
                foreach (GameObject gameObject in _level.GameObjects)
                {
                    if (gameObject.IsCreature && gameObject.IsAlly)
                    {
                        if ((float)Metrics.GetDistance(Position, _level.Player.Position) <= AggrRange)
                            Go(_level.GetObjectCoord(gameObject));
                    }
                }

                if ((float)Metrics.GetDistance(Position, _level.Player.Position) <= AggrRange)
                    Go(_level.GetObjectCoord(_level.Player));
            }

            Walk();
        }

        public override bool OnCollision(GameObject gameObject)
        {
            if (gameObject.IsCreature)
            {
                GOCreature creature = (GOCreature) gameObject;
                if (creature.IsAlly)
                {
                    Attack(gameObject);

                    return false;
                }
            }
            else if (gameObject.IsPlayer)
            {
                Attack(gameObject);

                return false;
            }

            return true;
        }
    }
}
